import socket, threading, Queue, select, wx

class MyGUI(wx.Frame):
    def __init__(self, qin, qout):
        wx.Frame.__init__(self, None, -1, "Chat", (-1,-1), (200,400), wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MINIMIZE_BOX |wx.MAXIMIZE_BOX))
        self.qIn = qin
        self.qOut = qout
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTime, self.timer)
        self.timer.Start(200)
        self.initGUI()

    def initGUI(self):
        self.chat = wx.TextCtrl(self, -1, "", pos=(0,0), size=(200,350), style=wx.TE_MULTILINE)
        self.chat.SetEditable(False)
        self.input = wx.TextCtrl(self, -1, "", pos=(0,350), size=(150,50), style=wx.TE_MULTILINE)
        self.button = wx.Button(self, -1, "Send", pos=(150,350), size=(50,50))
        self.button.Bind(wx.EVT_BUTTON, self.onClick, self.button)   
        self.Show()
        
    def onClick(self, evt):
        talk = self.input.GetValue()
        self.input.SetValue("")
        self.qOut.put(talk)
        
    def onTime(self, evt):
        while(self.qIn.qsize()):
            chat = self.qIn.get(0)
            self.chat.SetValue(self.chat.GetValue()+chat)
        
def recvChat(name, qin, qout):
    sock = socket.socket()
    sock.connect(("localhost", 5005))
    sock.send(name)
    ans = sock.recv(32)
    print ans
    input = [sock]
    while(True):
        recv, send, err = select.select(input, [], [], 0.5)
        for r in recv:
            msg = r.recv(64)
            qout.put(msg)
            print msg,
        if(qin.qsize()):
            sock.send(qin.get(0)+"\r\n")